package com.bc004346.travelus.Activity;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.bc004346.travelus.Fragment.FlightDetailsFragment;
import com.bc004346.travelus.Fragment.NavigationDrawerFragment;
import com.bc004346.travelus.Fragment.PbtFlightsFragment;
import com.bc004346.travelus.Model.Domain.Flight;
import com.bc004346.travelus.R;
import com.google.analytics.tracking.android.EasyTracker;

import static com.bc004346.travelus.Utility.log;

/**
 * Created by Renats on 3/16/14.
 * PBT Tracker
 */
public class PbtTrackerActivity extends Activity
        implements PbtFlightsFragment.OnFlightSelectionChangeListener {

    //<editor-fold desc="Members">

    private NavigationDrawerFragment mNavigationDrawer;

    //</editor-fold>
    //<editor-fold desc="Overrides">

    @Override
    protected void onStart() {
        super.onStart();
        EasyTracker.getInstance(this).activityStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EasyTracker.getInstance(this).activityStop(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pbt_tracker);

        try {
            mNavigationDrawer = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fullscreenDrawer);
            if (mNavigationDrawer != null)
                mNavigationDrawer.init();
        } catch (Exception e) {
            log(e, this);
        }
    }

    @Override
    public void onFlightSelectionChanged(Flight flight) {
        try {
            FragmentManager fm = getFragmentManager();
            FlightDetailsFragment descriptionFragment =
                    (FlightDetailsFragment) fm.findFragmentById(R.id.flightDetailsFragment);
            if (descriptionFragment != null)
                descriptionFragment.setFlightDetails(flight);
        } catch (Exception e) {
            log(e, this);
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mNavigationDrawer.syncState();
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        mNavigationDrawer.handleOnPrepareOptionsMenu(menu);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        mNavigationDrawer.syncState();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        mNavigationDrawer.handleOnOptionsItemSelected(item);
        return super.onOptionsItemSelected(item);
    }

    //</editor-fold>
}
